package com.xbongbong.paas.service.field;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.util.ItemUtil;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.SelectProductEnum;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/04/01 10:42
 */
@Service("comboAnalysisHandlerImpl")
public class ComboAnalysisHandlerImpl extends AbstractFieldAnalysisHandler {
    @Override
    public List<Integer> fieldType() {
        return Arrays.asList(FieldTypeEnum.RADIO_GROUP.getType(), FieldTypeEnum.COMBO.getType());
    }

    @Override
    public Object getValue(FieldAttrEntity fieldAttrEntity, PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAnalysisPoJo fieldAnalysisPoJo) throws XbbException {
        boolean invisibleOfScope = fieldAnalysisPoJo.isInvisibleOfScope();
        if (Objects.equals(fieldAnalysisPoJo.getDetailFlag(), 1) && Objects.equals(fieldAttrEntity.getSaasAttr(), SelectProductEnum.UNIT.getSaasAttr())) {
            if (invisibleOfScope) {
                return StringConstant.INVISIBLE_VALUE;
            } else {
               return data.get(fieldAttrEntity.getAttr());
            }
        } else {
            return ItemUtil.getComBoValue(fieldAttrEntity, data);
        }
    }
}
